//
//  MeHeaderView.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/8.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "MeHeaderView.h"
#import "YHMarcros.h"


@interface MeHeaderView ()
@property (nonatomic, strong)UIImageView * m_imgBackgroundView;
@property (nonatomic, strong)UIButton *m_btnSetting;
@property (nonatomic, strong)UIButton *m_btnBack;
@property (nonatomic, strong)UIImageView *m_imgIcon;
@property (nonatomic, strong)UILabel *m_lblName;
@property (nonatomic, strong)UILabel *m_lblTelNum;
@end
@implementation MeHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    [self addSubview:self.m_imgBackgroundView];
    [self.m_imgBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self addSubview:self.m_btnBack];
    [self.m_btnBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kIphoneSize_Height(30));
        make.leading.equalTo(self).offset(kIphoneSize_Widith(20));
        make.width.height.equalTo(44);
    }];
    [self addSubview:self.m_btnSetting];
    [self.m_btnSetting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kIphoneSize_Height(30));
        make.trailing.equalTo(self).offset(kIphoneSize_Widith(-20));
        make.width.height.equalTo(44);
    }];
    
    [self addSubview:self.m_imgIcon];
    [self.m_imgIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        
    }];

    [self addSubview:self.m_lblTelNum];
    [self.m_lblTelNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.m_imgIcon);
        make.top.equalTo(self.m_imgIcon.bottom).offset(kIphoneSize_Height(10));
    }];

    [self addSubview:self.m_lblName];
    [self.m_lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.m_imgIcon);
        make.top.equalTo(self.m_lblTelNum.bottom).offset(kIphoneSize_Height(5));

    }];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconClick:)];
    [self.m_imgIcon addGestureRecognizer:tapGesture];
}


#pragma mark - aciton
- (void)iconClick:(UITapGestureRecognizer *)tapGesture
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(meHeaderViewDidClick:)] ) {
        [self.delegate meHeaderViewDidClick:MeHeaderViewClickTypeIcon];
    }
}
- (void)btnClick:(UIButton *)btn {
    
    if (btn == self.m_btnBack){
        if (self.delegate && [self.delegate respondsToSelector:@selector(meHeaderViewDidClick:)] ) {
            [self.delegate meHeaderViewDidClick:MeHeaderViewClickTypeBack];
        }
    }else if (btn == self.m_btnSetting) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(meHeaderViewDidClick:)] ) {
            [self.delegate meHeaderViewDidClick:MeHeaderViewClickTypeSetting];
        }
    }
}

- (void)setStrPhone:(NSString *)strPhone
{
    _strPhone = [strPhone copy];
    NSMutableString *originTel = [NSMutableString stringWithString:_strPhone];
    NSString *replaceStr = @"****";
    NSRange range =  NSMakeRange(3, replaceStr.length);
    [originTel replaceCharactersInRange:range withString:replaceStr] ;
    self.m_lblTelNum.text = originTel;
}

- (void)setName:(NSString *)name{
    self.m_lblName.text = name;
}
#pragma mark - lazy loading

- (UIImageView *)m_imgBackgroundView
{
    if(!_m_imgBackgroundView){
        _m_imgBackgroundView = [[UIImageView alloc]init];
        _m_imgBackgroundView.image = [UIImage imageNamed:@"me_background"];
    }
    return _m_imgBackgroundView;
}

- (UIButton *)m_btnSetting
{
    if (!_m_btnSetting) {
        _m_btnSetting = [[UIButton alloc]init];
        [_m_btnSetting setImage:[UIImage imageNamed:@"me_icon_setting"] forState:UIControlStateNormal];
        [_m_btnSetting addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_m_btnSetting setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    }
    return _m_btnSetting;
}

- (UIButton *)m_btnBack
{
    if (!_m_btnBack) {
        _m_btnBack = [[UIButton alloc]init];
        [_m_btnBack setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
        [_m_btnBack addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_m_btnBack setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    }
    return _m_btnBack;
}
- (UIImageView *)m_imgIcon
{
    if(!_m_imgIcon){
        _m_imgIcon = [[UIImageView alloc]init];
        _m_imgIcon.image = [UIImage imageNamed:@"me_icon"];
        _m_imgIcon.userInteractionEnabled = YES;
        
    }
    return _m_imgIcon;
}

- (UILabel *)m_lblName
{
    if (!_m_lblName) {
        _m_lblName = [[UILabel alloc]init];
        _m_lblName.text = @"娜娜";
        _m_lblName.textAlignment = NSTextAlignmentCenter;
        _m_lblName.textColor = [UIColor whiteColor];
    }
    return _m_lblName;
}

- (UILabel *)m_lblTelNum
{
    if (!_m_lblTelNum) {
        _m_lblTelNum = [[UILabel alloc]init];
        _m_lblTelNum.textAlignment = NSTextAlignmentCenter;
        _m_lblTelNum.textColor = [UIColor whiteColor];
    }
    return _m_lblTelNum;
}
@end
